export default {//创建并导出vuex
    namespaced:true,
    state:{//存放保存在仓库中的数据
        site:null,
        user:null,
        consignee:null,
        phone:null,
        siteId:null,
        cartId:[],//存储进入订单页的购物车商品的id
        product:{
            prices:null,
            quantity:0,
            item:[],
        },
    },
    getters:{//存放state中的数据为基础创建的计算属性

    },
    mutations:{//更改state中的数据，只有mutations有资格
        get(state,payload){
            state.sp = payload;
        }
    },
    actions:{//执行一些异步耗时操作，比如请求数据
        //保存地址组件发送ajax
        saveSite({state,commit, rootState}, payload){
            //console.log(rootState)
            if(!payload.toggle){
                if(rootState.order.all.length === 0){//如果没有地址第一次添加是默认地址
                    payload.activate = true;
                    payload.none = true;
                }
                rootState.$http.post("/site/addSite", {data: payload})//路由和传参//保存
                    .then(result => {
                    });
            }else{
                rootState.$http.post("/site/addSite", {data: payload})//路由和传参//切换
                    .then(result => {
                    });
            }
        },
        del({state,commit, rootState: {$http}}, payload){
            let activate = false;
            let i = 0;
            state.all.filter((item,idx)=>{
                if(item.id === payload){
                    if(idx === 0 && state.all.length > 1){
                        i = 1;
                    }else if(state.all.length === 1){i = null}
                    else{
                        i = 0;
                    }
                    item.activate === 1 ? activate = true : activate = false;
                }
            });
            if(i != null){
                i = state.all[i].id;
            }
            $http.post("/site/del", {id: payload,activate,i})//路由和传参
                .then(result => {console.log(result)});
        }
    },
};